home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / clib02.arc / WILDCARD.C < prev   
Encoding:
Text File  |  1985-10-18  |  1.8 KB  |  75 lines

  1. /*
  2. *    Wildcard matching routine for MSDOS V2.0, Lattice C V2.0.
  3. *        Ross Nelson
  4. *        10 November 1983
  5. *
  6. *    Requires "find1st" and "findnext" from FIND.ASM.  User passes
  7. *    in a legal MSDOS wildcard, an attribute bit mask, and a buffer.
  8. *    On the first call, the wildcard is passed to DOS, and the
  9. *    routine returns 0 if there was no match.  If a match occurs,
  10. *    the first matching file name in the directory is copied into
  11. *    the buffer.  Subsequent calls ignore the first two parameters
  12. *    and copy the next succeding files to the buffer at the third
  13. *    parameter.  When no further matches exist the function returns
  14. *    FALSE (0).  Typical usage is:
  15. *
  16. *        if (!wildcard(pattern, 0x10, filename))
  17. *            /* error, no match */
  18. *        else
  19. *            do {
  20. *                /* process filename */
  21. *            } while (wildcard(0, 0, filename));
  22. *
  23. *
  24. */
  25.  
  26. #include <ctype.h>
  27.  
  28. struct dta {
  29.     char reserve[21];
  30.     char attrib;
  31.     unsigned time;
  32.     unsigned date;
  33.     long size;
  34.     char name[13];
  35.     };
  36.  
  37. static struct dta info;
  38. static int repeat = 0;
  39. static char dir[64];
  40.  
  41. int wildcard (wc, attr, file)
  42. char *wc, attr, *file;
  43. {
  44.     if (!repeat) {
  45.         get_header (wc, dir);    /* save directory prefix */
  46.         repeat = find1st (wc, attr, &info);
  47.         }
  48.     else
  49.         repeat = findnext (&info);
  50.     if (repeat) {
  51.         strcpy (file, dir);    /* directory prefix */
  52.         strcat (file, info.name);  /* file name */
  53.         while (*file) {
  54.             *file = tolower (*file);
  55.             file++;
  56.             }
  57.         }
  58.     return (int) repeat;
  59. }
  60.  
  61. static get_header (s, head)
  62. char *s, *head;
  63. {
  64. char *ptr;
  65.  
  66.     /* Strip device or directory header e.g. a: or \etc\ */
  67.     ptr = s + strlen (s);
  68.     while (s < ptr--)
  69.         if ((*ptr == '\\') || (*ptr == '/') || (*ptr == ':'))
  70.             break;
  71.     while (s <= ptr)
  72.         *head++ = *s++;
  73.     *head = '\0';
  74. }
  75.